package com.alm.handle.util;


import com.alm.handle.core.domain.entity.SysDevParam;
import com.alm.handle.core.domain.entity.rq.MonitorParamsDTO;
import com.alm.handle.core.domain.entity.rq.SetDevParamRq;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.ByteBuffer;
import java.util.List;

/**
 *
 * @create 2023-04-11 19:11:51
 */
public class DevMonitorUtils {
    /**
     * 一个 MonitorParamsDTO.MonitorParams对象至少占4个字节
     */
    public static final Integer MIN_BYTE = 4;

    public static final Byte PARAM_ID = 0x06;

    public static byte[] convert(List<SetDevParamRq> list) {
        ByteBuf buffer= Unpooled.buffer(1);
        byte[] array;
        switch (list.get(0).getCode()){
            case 50:
                buffer.writeByte(0xF4);
                buffer.writeByte(list.get(0).getThreshold());
                 array = new byte[buffer.readableBytes()];
                buffer.readBytes(array);
                buffer.release();
                return array;
            case 51:
                buffer.writeByte(0xF4);
                buffer.writeByte(list.get(0).getThreshold());
                buffer.writeByte(list.get(1).getThreshold());
                array = new byte[buffer.readableBytes()];
                buffer.readBytes(array);
                buffer.release();
                return array;
            case 52:
                buffer.writeByte(0xB1);
                buffer.writeByte(list.get(0).getThreshold());
                buffer.writeShort(list.get(1).getThreshold());
                array = new byte[buffer.readableBytes()];
                buffer.readBytes(array);
                buffer.release();
                return array;
            default:
                buffer.writeByte(0x06);
                for(SetDevParamRq  params1:list){
                    ByteBuf byteBuf1= Unpooled.buffer(1);
                    if(params1.getCode()==0x14){
                        byteBuf1.writeByte(params1.getCode());
                        byteBuf1.writeByte(params1.getThreshold().byteValue());
                    }else{
                        byteBuf1.writeByte(params1.getCode());
                        byteBuf1.writeInt(params1.getThreshold());
                    }
                    int number=byteBuf1.readableBytes();
                    buffer.writeByte(number);
                    buffer.writeBytes(byteBuf1);
                    byteBuf1.release();
                }
                break;
        }
        array = new byte[buffer.readableBytes()];
        buffer.readBytes(array);
        buffer.release();
        return array;

    }
    public static byte[] convertCC(List<MonitorParamsDTO.MonitorParams> list) {
        int sumSize = list.stream().mapToInt(MonitorParamsDTO.MonitorParams::getLength).sum() + list.size();
        ByteBuf byteBuf = Unpooled.buffer(sumSize + 1);
        byteBuf.writeBytes(ByteBuffer.allocate(1).put(PARAM_ID).array());
        list.forEach(t -> {
            ByteBuffer buffer = getByteBuffer(t,t.getLength());
            byteBuf.writeBytes(buffer.array());
        });
        final byte[] array = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(array);
        byteBuf.release();
        return array;

    }
    private static ByteBuffer getByteBuffer(MonitorParamsDTO.MonitorParams monitorParams, Byte length) {
        int instDataLength = length + 1;
        ByteBuffer buffer = ByteBuffer.allocate(instDataLength);
        buffer.put(length);
        buffer.put(monitorParams.getType());
        final Integer threshold = monitorParams.getThreshold();
        buffer = instDataLength > MIN_BYTE ? buffer.putInt(threshold) : buffer.putShort(threshold.shortValue());
        return buffer;
    }
}
